{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "35262a93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "61eadb5e7bd449298e063dca855701ed",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/86 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "782cdc3c6c214ac1b7139e085c8fa716",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/77 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from datetime import datetime,timedelta\n",
    "from 数据处理函数 import *\n",
    "from const import get_cat\n",
    "from tqdm.notebook import tqdm\n",
    "import os\n",
    "import re\n",
    "import tushare as ts\n",
    "token = '1501ffe708345cffa38d9bbc0bd371e93b4b7412e7a8e1f811d3c442'\n",
    "ts.set_token(token)\n",
    "pro = ts.pro_api(token)\n",
    "\n",
    "交易所s = ['CFFEX','DCE','CZCE','SHFE','INE','GFEX']\n",
    "dds = []\n",
    "for 交易所 in 交易所s:\n",
    "    dd = pro.fut_basic(exchange=交易所, fut_type='1')\n",
    "    dds.append(dd)\n",
    "df_所有合约 = pd.concat(dds)\n",
    "df_所有合约['list_date'] = pd.to_datetime(df_所有合约['list_date'])\n",
    "df_所有合约['delist_date'] = pd.to_datetime(df_所有合约['delist_date'])\n",
    "df_所有合约 = df_所有合约[df_所有合约['delist_date']>'2010-01-01']\n",
    "df_所有合约 = df_所有合约.sort_values(by=['ts_code']).reset_index(drop=True)\n",
    "df_所有合约['name'] = df_所有合约['name'].apply(lambda x:re.sub(r'[^\\u4e00-\\u9fa5]', '', x))\n",
    "all_codes = list(df_所有合约['ts_code'])\n",
    "code_delist_date_dict = dict(zip(df_所有合约['ts_code'].apply(lambda x:x.split('.')[0]),df_所有合约['delist_date']))\n",
    "\n",
    "df_cat = get_cat()\n",
    "期货前缀ss = os.listdir('D:/tushare_database/dailydata')\n",
    "期货前缀ss = [x.split('.')[0] for x in 期货前缀ss]\n",
    "# 期货前缀ss = list(set(期货前缀ss) - set(list(df_cat[df_cat['category']=='fin'].index)))\n",
    "\n",
    "期货前缀ss2 = []\n",
    "for 期货前缀 in tqdm(期货前缀ss):\n",
    "    df = get_df(期货前缀)\n",
    "    df_close = df.pivot(index='t', columns='code', values='close')\n",
    "#     if (df_close.notnull().sum(1) != 0).sum() > 250 and df_close.columns[-1] > '2301':\n",
    "    if (df_close.notnull().sum(1) != 0).sum() > 250:\n",
    "        期货前缀ss2.append(期货前缀)\n",
    "        \n",
    "dd_qty_param = pd.read_csv('basicdata/qty_param.csv',index_col=0)\n",
    "原始字母字典 = dict(zip([x.upper() for x in list(dd_qty_param.index)],list(dd_qty_param.index)))\n",
    "\n",
    "字母s = []\n",
    "最新价格s = []\n",
    "for 期货前缀 in tqdm(期货前缀ss2):\n",
    "    df = get_df(期货前缀)\n",
    "    dd_amount = df.pivot(index='t',columns='code',values='amount')\n",
    "    df_close = df.pivot(index='t', columns='code', values='close')\n",
    "    当前主力代码 = pd.Series(dd_amount.columns[np.argsort(-dd_amount.values,axis=1)[:,0]],index=dd_amount.index).shift(1)\n",
    "    最新价格 = df_close.apply(lambda x: x.get(当前主力代码.get(x.name)), axis=1)[-1]\n",
    "    if 原始字母字典.get(期货前缀):\n",
    "        字母s.append(原始字母字典.get(期货前缀))\n",
    "        最新价格s.append(最新价格)\n",
    "    \n",
    "最新价格字典 = dict(zip(字母s,最新价格s))\n",
    "\n",
    "dd_qty_param = pd.read_csv('basicdata/qty_param.csv',index_col=0)\n",
    "dd_qty_param['price'] = pd.Series(dd_qty_param.index,index=dd_qty_param.index).apply(lambda x:最新价格字典.get(x))\n",
    "dd_qty_param['per_margin'] = dd_qty_param['multiplier']*dd_qty_param['price']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f7a7e632",
   "metadata": {},
   "outputs": [],
   "source": [
    "因子投资比例 = \\\n",
    "{'展期因子-三月': 0.0399,\n",
    " '展期因子-一月': 0.043,\n",
    " '展期因子-二月': 0.0506,\n",
    " '开盘因子-三月': 0.0816,\n",
    " '开盘因子-一月': 0.08,\n",
    " '动量因子1': 0.0394,\n",
    " '动量因子2': 0.0519,\n",
    " '偏度因子': 0.0663,\n",
    " '动量因子3': 0.1222,\n",
    " '反转因子': 0.1046,\n",
    " '基差因子': 0.077,\n",
    " '基差因子3': 0.0599,\n",
    " 'ATR因子': 0.1193,\n",
    " '价格因子': 0.0642,\n",
    " '行业展期因子-三月': 0.05/3,\n",
    " '行业展期因子-一月': 0.05/3,\n",
    " '行业展期因子-二月': 0.05/3,\n",
    " '基差变化因子':0.05/2,\n",
    " '展期变化因子':0.05/2\n",
    "}\n",
    "\n",
    "因子投资比例 = \\\n",
    "{'展期因子-三月': 0.037,\n",
    " '展期因子-一月': 0.04,\n",
    " '展期因子-二月': 0.045,\n",
    " '开盘因子-三月': 0.084,\n",
    " '开盘因子-一月': 0.067,\n",
    " '动量因子1': 0.033,\n",
    " '动量因子2': 0.044,\n",
    " '偏度因子': 0.058,\n",
    " '动量因子3': 0.108,\n",
    " '反转因子': 0.101,\n",
    " '基差因子': 0.07,\n",
    " '基差因子3': 0.049,\n",
    " 'ATR因子': 0.129,\n",
    " '价格因子': 0.057,\n",
    " 'curveRSI因子': 0.043,\n",
    " '平均基差变化因子': 0.034,\n",
    " '行业展期因子-三月': 0.05/3,\n",
    " '行业展期因子-一月': 0.05/3,\n",
    " '行业展期因子-二月': 0.05/3,\n",
    " '基差变化因子':0.05/2,\n",
    " '展期变化因子':0.05/2\n",
    "}\n",
    "\n",
    "因子投资比例 = \\\n",
    "{'展期因子-三月': 0.03,\n",
    " '展期因子-一月': 0.032,\n",
    " '展期因子-二月': 0.037,\n",
    " '开盘因子-三月': 0.068,\n",
    " '开盘因子-一月': 0.063,\n",
    " '动量因子1': 0.027,\n",
    " '动量因子2': 0.038,\n",
    " '偏度因子': 0.048,\n",
    " '动量因子3': 0.092,\n",
    " '反转因子': 0.081,\n",
    " '基差因子': 0.072,\n",
    " '基差因子3': 0.046,\n",
    " 'ATR因子': 0.116,\n",
    " '价格因子': 0.047,\n",
    " '展期变化因子': 0.137,\n",
    " 'curveRSI因子': 0.037,\n",
    " '平均基差变化因子': 0.03,\n",
    " '行业展期因子-三月': 0.05/3,\n",
    " '行业展期因子-一月': 0.05/3,\n",
    " '行业展期因子-二月': 0.05/3}\n",
    "\n",
    "方向s = {\n",
    "    '展期因子-三月':-1,\n",
    "    '展期因子-一月':-1,\n",
    "    '展期因子-二月':-1,\n",
    "    '开盘因子-三月':-1,\n",
    "    '开盘因子-一月':-1,\n",
    "    '动量因子1':1,\n",
    "    '动量因子2':1,\n",
    "    '偏度因子':-1,\n",
    "    '动量因子3':1,\n",
    "    '反转因子':-1,\n",
    "    '基差因子':1,\n",
    "    '基差因子3':1,\n",
    "    'ATR因子':-1,\n",
    "    '价格因子':-1,\n",
    "    '基差变化因子':-1,\n",
    "    '展期变化因子':1,\n",
    "    'curveRSI因子':-1,\n",
    "    '平均基差变化因子':-1\n",
    "}\n",
    "\n",
    "the_dates = {\n",
    "    '展期因子-三月':'2023-12-31',\n",
    "    '展期因子-一月':'2023-10-31',\n",
    "    '展期因子-二月':'2023-11-30',\n",
    "    '开盘因子-三月':'2023-12-31',\n",
    "    '开盘因子-一月':'2023-10-31',\n",
    "    '动量因子1':'2023-12-31',\n",
    "    '动量因子2':'2023-12-31',\n",
    "    '偏度因子':'2023-12-31',\n",
    "    '动量因子3':'2023-12-31',\n",
    "    '反转因子':'2023-12-31',\n",
    "    '基差因子':'2023-12-31',\n",
    "    '基差因子3':'2023-12-31',\n",
    "    'ATR因子':'2023-12-31',\n",
    "    '价格因子':'2023-12-31',\n",
    "    '基差变化因子':'2023-12-31',\n",
    "    '展期变化因子':'2023-12-31',\n",
    "    'curveRSI因子':'2023-12-31',\n",
    "    '平均基差变化因子':'2023-12-31',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ddaff52f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0509999999999997"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(因子投资比例.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6daedfc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5d642f9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "允许交易的品种 = \\\n",
    "['AG', 'AL', 'AU', 'BU', 'C', 'CF', 'CU', 'EB', 'FG', 'FU',\n",
    " 'HC', 'I', 'L', 'LH', 'LU', 'M', 'MA', 'NI', 'NR', 'OI',\n",
    " 'P', 'PF', 'PG', 'PP', 'RB', 'RM', 'RU', 'SA', 'SC', 'SF',\n",
    " 'SN', 'SP', 'SR', 'SS', 'T', 'TA', 'TF', 'TS', 'UR', 'V',\n",
    " 'Y', 'ZN']\n",
    "name_dict = dict(zip(df_所有合约['fut_code'],df_所有合约['name']))\n",
    "name_dict.update({'IC':'中证500','IF':'沪深300','IH':'上证50','IM':'中证1000',\n",
    "                  'PG':'液化气','T':'10年国债','TA':'PTA','TF':'5年国债',\n",
    "                  'TL':'30年国债','TS':'2年国债','V':'PVC','MA':'甲醇'})\n",
    "del name_dict['SCTAS']\n",
    "del name_dict['ME']\n",
    "name_dict2 = {v: k for k, v in name_dict.items()}\n",
    "\n",
    "允许交易的品种_中文 = [name_dict.get(x) for x in 允许交易的品种]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7843c8eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "能源 = ['原油','燃油','低硫燃料油','液化气']\n",
    "化工 = ['玻璃','纯碱','烧碱','沥青','甲醇','PTA','对二甲苯','短纤','苯乙烯','乙二醇','聚丙烯','PVC','塑料','橡胶','尿素','纸浆']\n",
    "建材 = ['螺纹钢','玻璃','PVC','胶合板','纤维板']\n",
    "钢铁 = ['铁矿石','螺纹钢','热轧卷板','不锈钢','线材']\n",
    "黑色 = ['铁矿石','螺纹钢','热轧卷板','焦炭','焦煤','硅铁','锰硅','不锈钢','线材']\n",
    "有色 = ['沪铜','沪铝','沪镍','沪锡','沪铅','沪锌','氧化铝','国际铜','工业硅','碳酸锂']\n",
    "煤炭 = ['焦炭','焦煤']\n",
    "贵金属 = ['沪金','沪银']\n",
    "油料 = ['豆一','豆二','豆油','棕榈油','菜油','花生']\n",
    "谷物 = ['豆粕','菜粕','玉米','粳米']\n",
    "农副 = ['郑棉','白糖','生猪','苹果','鸡蛋','玉米淀粉','红枣','棉纱']\n",
    "\n",
    "能源 = list(set(能源)&set(允许交易的品种_中文))\n",
    "化工 = list(set(化工)&set(允许交易的品种_中文))\n",
    "建材 = list(set(建材)&set(允许交易的品种_中文))\n",
    "钢铁 = list(set(钢铁)&set(允许交易的品种_中文))\n",
    "黑色 = list(set(黑色)&set(允许交易的品种_中文))\n",
    "有色 = list(set(有色)&set(允许交易的品种_中文))\n",
    "煤炭 = list(set(煤炭)&set(允许交易的品种_中文))\n",
    "贵金属 = list(set(贵金属)&set(允许交易的品种_中文))\n",
    "油料 = list(set(油料)&set(允许交易的品种_中文))\n",
    "谷物 = list(set(谷物)&set(允许交易的品种_中文))\n",
    "农副 = list(set(农副)&set(允许交易的品种_中文))\n",
    "\n",
    "行业s = [能源,化工,建材,钢铁,黑色,有色,煤炭,贵金属,油料,谷物,农副]\n",
    "行业名字 = ['能源','化工','建材','钢铁','黑色','有色','煤炭','贵金属','油料','谷物','农副']\n",
    "行业名字_行业品种字典 = dict(zip(行业名字,行业s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45f53d91",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "aa6e6ea6",
   "metadata": {},
   "outputs": [],
   "source": [
    "factor_folder = 'factors_20231231'\n",
    "因子名s = [\n",
    "    '展期因子-三月','展期因子-一月','展期因子-二月',\n",
    "#     '行业展期因子-三月','行业展期因子-一月','行业展期因子-二月',\n",
    "    '开盘因子-三月','开盘因子-一月',\n",
    "    '动量因子1','动量因子2',\n",
    "    '偏度因子','动量因子3',\n",
    "    '反转因子','基差因子',\n",
    "    '基差因子3',\n",
    "    'ATR因子','价格因子',\n",
    "#     '基差变化因子',\n",
    "    '展期变化因子',\n",
    "    'curveRSI因子','平均基差变化因子',\n",
    "]\n",
    "\n",
    "df_因子s = {}\n",
    "for 因子名 in 因子名s:\n",
    "    dd = pd.read_csv(f'{factor_folder}/{因子名}.csv',index_col=0)\n",
    "    dd.index = pd.to_datetime(dd.index)\n",
    "    df_因子s.update({因子名:dd})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "66c01297",
   "metadata": {},
   "outputs": [],
   "source": [
    "account_name = 'DS'\n",
    "账户资金 = 2800000\n",
    "\n",
    "# account_name = 'CP'\n",
    "# 账户资金 = 8000000\n",
    "\n",
    "杠杆 = 9\n",
    "总投资额 = 账户资金*杠杆\n",
    "\n",
    "topp = 0.8\n",
    "botp = 0.2\n",
    "\n",
    "dd_qtys = []\n",
    "投资比例s = []\n",
    "for 因子名 in 因子名s:\n",
    "    方向 = 方向s[因子名]\n",
    "\n",
    "    dd = df_因子s[因子名].copy()\n",
    "\n",
    "    if 方向 == -1:\n",
    "        dd1 = dd.rank(1,pct=True) < botp\n",
    "        dd2 = dd.rank(1,pct=True) > topp\n",
    "    elif 方向 == 1:\n",
    "        dd1 = dd.rank(1,pct=True) > topp\n",
    "        dd2 = dd.rank(1,pct=True) < botp\n",
    "\n",
    "    做多的品种 = dd1.loc[the_dates[因子名]][dd1.loc[the_dates[因子名]]].index.tolist()\n",
    "    做空的品种 = dd2.loc[the_dates[因子名]][dd2.loc[the_dates[因子名]]].index.tolist()\n",
    "    \n",
    "#     if 'eg' in 做多的品种:\n",
    "#         print(因子名,'1 eg')\n",
    "#     elif 'eg' in 做空的品种:\n",
    "#         print(因子名,'2 eg')\n",
    "    投资比例 = 因子投资比例[因子名]\n",
    "    投资比例s.append(投资比例)\n",
    "    因子投资额 = 总投资额*投资比例\n",
    "    做多单个品种投资额 = 因子投资额/2/len(做多的品种)\n",
    "    做空单个品种投资额 = 因子投资额/2/len(做空的品种)\n",
    "    做多qty = [做多单个品种投资额/dd_qty_param.loc[x,'per_margin'] for x in 做多的品种]\n",
    "    做空qty = [-做空单个品种投资额/dd_qty_param.loc[x,'per_margin'] for x in 做空的品种]\n",
    "    dd_qty1 = pd.DataFrame({'品种':做多的品种,'qty':做多qty})\n",
    "    dd_qty2 = pd.DataFrame({'品种':做空的品种,'qty':做空qty})\n",
    "    dd_qty = pd.concat([dd_qty1,dd_qty2]).reset_index(drop=True)\n",
    "    dd_qty['因子'] = 因子名\n",
    "    dd_qtys.append(dd_qty)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3139cc00",
   "metadata": {},
   "outputs": [],
   "source": [
    "行业展期因子 = pd.read_csv('行业展期因子20231231.csv',index_col=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2da5a919",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>品种</th>\n",
       "      <th>qty</th>\n",
       "      <th>因子</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CF</td>\n",
       "      <td>0.362342</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>FG</td>\n",
       "      <td>0.975780</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MA</td>\n",
       "      <td>1.151789</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>PF</td>\n",
       "      <td>0.762943</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>RM</td>\n",
       "      <td>0.975610</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>SA</td>\n",
       "      <td>0.684932</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>SF</td>\n",
       "      <td>0.841346</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>SR</td>\n",
       "      <td>0.443951</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>TA</td>\n",
       "      <td>0.943396</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>UR</td>\n",
       "      <td>0.662879</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>ag</td>\n",
       "      <td>-0.312361</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>al</td>\n",
       "      <td>0.287106</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>au</td>\n",
       "      <td>-0.058127</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>bu</td>\n",
       "      <td>0.765446</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>c</td>\n",
       "      <td>1.160381</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>cu</td>\n",
       "      <td>0.081254</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>eb</td>\n",
       "      <td>0.660066</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>fu</td>\n",
       "      <td>0.956284</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>hc</td>\n",
       "      <td>1.361206</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>i</td>\n",
       "      <td>0.572012</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>l</td>\n",
       "      <td>0.676329</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>lh</td>\n",
       "      <td>0.127598</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>lu</td>\n",
       "      <td>0.686948</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>m</td>\n",
       "      <td>0.845155</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>ni</td>\n",
       "      <td>0.223624</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>p</td>\n",
       "      <td>0.395145</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>pg</td>\n",
       "      <td>0.291910</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>pp</td>\n",
       "      <td>0.740741</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>rb</td>\n",
       "      <td>1.632517</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>ru</td>\n",
       "      <td>0.197601</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>sc</td>\n",
       "      <td>0.051594</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>sn</td>\n",
       "      <td>0.132013</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>sp</td>\n",
       "      <td>0.496454</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>ss</td>\n",
       "      <td>0.818713</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>v</td>\n",
       "      <td>1.270057</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>y</td>\n",
       "      <td>0.372241</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>zn</td>\n",
       "      <td>0.259921</td>\n",
       "      <td>行业展期因子</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    品种       qty      因子\n",
       "0   CF  0.362342  行业展期因子\n",
       "1   FG  0.975780  行业展期因子\n",
       "2   MA  1.151789  行业展期因子\n",
       "3   PF  0.762943  行业展期因子\n",
       "4   RM  0.975610  行业展期因子\n",
       "5   SA  0.684932  行业展期因子\n",
       "6   SF  0.841346  行业展期因子\n",
       "7   SR  0.443951  行业展期因子\n",
       "8   TA  0.943396  行业展期因子\n",
       "9   UR  0.662879  行业展期因子\n",
       "10  ag -0.312361  行业展期因子\n",
       "11  al  0.287106  行业展期因子\n",
       "12  au -0.058127  行业展期因子\n",
       "13  bu  0.765446  行业展期因子\n",
       "14   c  1.160381  行业展期因子\n",
       "15  cu  0.081254  行业展期因子\n",
       "16  eb  0.660066  行业展期因子\n",
       "17  fu  0.956284  行业展期因子\n",
       "18  hc  1.361206  行业展期因子\n",
       "19   i  0.572012  行业展期因子\n",
       "20   l  0.676329  行业展期因子\n",
       "21  lh  0.127598  行业展期因子\n",
       "22  lu  0.686948  行业展期因子\n",
       "23   m  0.845155  行业展期因子\n",
       "24  ni  0.223624  行业展期因子\n",
       "25   p  0.395145  行业展期因子\n",
       "26  pg  0.291910  行业展期因子\n",
       "27  pp  0.740741  行业展期因子\n",
       "28  rb  1.632517  行业展期因子\n",
       "29  ru  0.197601  行业展期因子\n",
       "30  sc  0.051594  行业展期因子\n",
       "31  sn  0.132013  行业展期因子\n",
       "32  sp  0.496454  行业展期因子\n",
       "33  ss  0.818713  行业展期因子\n",
       "34   v  1.270057  行业展期因子\n",
       "35   y  0.372241  行业展期因子\n",
       "36  zn  0.259921  行业展期因子"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one_split = sum([len(x) for x in 行业s])\n",
    "\n",
    "pp = []\n",
    "for fac in 行业展期因子.columns:\n",
    "    for 行业 in 行业名字:\n",
    "        pp += [[原始字母字典.get(name_dict2.get(x)),因子投资比例.get(fac)*行业展期因子.loc[行业,fac]/one_split]\n",
    "               for x in 行业名字_行业品种字典.get(行业) if 原始字母字典.get(name_dict2.get(x))]\n",
    "        \n",
    "df_pp = pd.DataFrame(pp)\n",
    "df_pp.columns = ['品种','额度']\n",
    "df_pp_qty = ((总投资额*df_pp.groupby(['品种']).sum())['额度']/dd_qty_param['per_margin']).dropna()\n",
    "df_pp_qty = df_pp_qty.reset_index()\n",
    "df_pp_qty.columns = ['品种','qty']\n",
    "df_pp_qty['因子'] = '行业展期因子'\n",
    "df_pp_qty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5d4c8b10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "净头寸市值: 82.17\n",
      "市值总额: 1313.64\n",
      "保证金总额: 154.4\n"
     ]
    }
   ],
   "source": [
    "dd_qtys2 = dd_qtys + [df_pp_qty]\n",
    "df_qty = pd.concat(dd_qtys2)\n",
    "# df_qty = pd.concat(dd_qtys)\n",
    "# df_qty['qty'] = df_qty['qty'].abs()\n",
    "df_qty = df_qty.groupby(['品种']).sum(numeric_only=True).sort_index()\n",
    "df_qty['qty取整'] = df_qty.round()\n",
    "df_qty['市值'] = (dd_qty_param['per_margin']*df_qty['qty取整']).dropna()/10000\n",
    "df_qty['保证金'] = (df_qty['市值']*dd_qty_param['margin']).dropna().abs()\n",
    "df_qty = df_qty.sort_values(by=['市值'])\n",
    "print('净头寸市值:',df_qty['市值'].sum().round(2))\n",
    "print('市值总额:',df_qty['市值'].abs().sum().round(2))\n",
    "print('保证金总额:',df_qty['保证金'].abs().sum().round(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "37dfe3fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "净头寸市值: 291.97\n",
      "市值总额: 3767.65\n",
      "保证金总额: 441.69\n"
     ]
    }
   ],
   "source": [
    "dd_qtys2 = dd_qtys + [df_pp_qty]\n",
    "df_qty = pd.concat(dd_qtys2)\n",
    "# df_qty = pd.concat(dd_qtys)\n",
    "# df_qty['qty'] = df_qty['qty'].abs()\n",
    "df_qty = df_qty.groupby(['品种']).sum(numeric_only=True).sort_index()\n",
    "df_qty['qty取整'] = df_qty.round()\n",
    "df_qty['市值'] = (dd_qty_param['per_margin']*df_qty['qty取整']).dropna()/10000\n",
    "df_qty['保证金'] = (df_qty['市值']*dd_qty_param['margin']).dropna().abs()\n",
    "df_qty = df_qty.sort_values(by=['市值'])\n",
    "print('净头寸市值:',df_qty['市值'].sum().round(2))\n",
    "print('市值总额:',df_qty['市值'].abs().sum().round(2))\n",
    "print('保证金总额:',df_qty['保证金'].abs().sum().round(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fda89183",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>qty</th>\n",
       "      <th>qty取整</th>\n",
       "      <th>市值</th>\n",
       "      <th>保证金</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>品种</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ru</th>\n",
       "      <td>-20.299224</td>\n",
       "      <td>-20.0</td>\n",
       "      <td>-283.4000</td>\n",
       "      <td>28.340000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>eg</th>\n",
       "      <td>-40.018067</td>\n",
       "      <td>-40.0</td>\n",
       "      <td>-177.1200</td>\n",
       "      <td>21.254400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>lh</th>\n",
       "      <td>-4.865111</td>\n",
       "      <td>-5.0</td>\n",
       "      <td>-109.7200</td>\n",
       "      <td>13.166400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sn</th>\n",
       "      <td>-4.939180</td>\n",
       "      <td>-5.0</td>\n",
       "      <td>-106.0500</td>\n",
       "      <td>14.847000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sp</th>\n",
       "      <td>-17.872340</td>\n",
       "      <td>-18.0</td>\n",
       "      <td>-101.5200</td>\n",
       "      <td>15.228000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SF</th>\n",
       "      <td>-29.447115</td>\n",
       "      <td>-29.0</td>\n",
       "      <td>-96.5120</td>\n",
       "      <td>11.581440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>v</th>\n",
       "      <td>-32.037195</td>\n",
       "      <td>-32.0</td>\n",
       "      <td>-94.0640</td>\n",
       "      <td>10.347040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>bc</th>\n",
       "      <td>-3.103022</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>-92.3100</td>\n",
       "      <td>9.231000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>nr</th>\n",
       "      <td>-8.476276</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>-89.3600</td>\n",
       "      <td>8.936000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>eb</th>\n",
       "      <td>-17.095710</td>\n",
       "      <td>-17.0</td>\n",
       "      <td>-72.1140</td>\n",
       "      <td>7.211400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>cu</th>\n",
       "      <td>-1.628555</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>-68.9200</td>\n",
       "      <td>8.270400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ss</th>\n",
       "      <td>-9.514620</td>\n",
       "      <td>-10.0</td>\n",
       "      <td>-68.4000</td>\n",
       "      <td>6.156000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>zn</th>\n",
       "      <td>-6.037596</td>\n",
       "      <td>-6.0</td>\n",
       "      <td>-64.6350</td>\n",
       "      <td>9.048900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ag</th>\n",
       "      <td>-6.033021</td>\n",
       "      <td>-6.0</td>\n",
       "      <td>-53.7840</td>\n",
       "      <td>6.454080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TA</th>\n",
       "      <td>-18.423181</td>\n",
       "      <td>-18.0</td>\n",
       "      <td>-53.4240</td>\n",
       "      <td>4.273920</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SM</th>\n",
       "      <td>-11.907152</td>\n",
       "      <td>-12.0</td>\n",
       "      <td>-38.2560</td>\n",
       "      <td>4.590720</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pb</th>\n",
       "      <td>-3.774488</td>\n",
       "      <td>-4.0</td>\n",
       "      <td>-31.7500</td>\n",
       "      <td>4.445000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pg</th>\n",
       "      <td>-2.989992</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>-28.7760</td>\n",
       "      <td>3.740880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CF</th>\n",
       "      <td>-3.038499</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>-23.1825</td>\n",
       "      <td>1.622775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>l</th>\n",
       "      <td>-5.120773</td>\n",
       "      <td>-5.0</td>\n",
       "      <td>-20.7000</td>\n",
       "      <td>2.277000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>al</th>\n",
       "      <td>-2.239426</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>-19.5050</td>\n",
       "      <td>2.340600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PF</th>\n",
       "      <td>-4.196185</td>\n",
       "      <td>-4.0</td>\n",
       "      <td>-14.6800</td>\n",
       "      <td>1.468000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>p</th>\n",
       "      <td>-1.721705</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>-14.1720</td>\n",
       "      <td>1.700640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>OI</th>\n",
       "      <td>-1.416196</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-7.9650</td>\n",
       "      <td>0.716850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>-1.063547</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-7.5220</td>\n",
       "      <td>0.676980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>au</th>\n",
       "      <td>0.161927</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0.649819</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.9860</td>\n",
       "      <td>0.598320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pp</th>\n",
       "      <td>2.116402</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.5600</td>\n",
       "      <td>0.831600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rb</th>\n",
       "      <td>1.885724</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.0040</td>\n",
       "      <td>1.040520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>3.690476</td>\n",
       "      <td>4.0</td>\n",
       "      <td>17.4720</td>\n",
       "      <td>1.572480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MA</th>\n",
       "      <td>9.230769</td>\n",
       "      <td>9.0</td>\n",
       "      <td>21.8790</td>\n",
       "      <td>1.750320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>jm</th>\n",
       "      <td>1.769536</td>\n",
       "      <td>2.0</td>\n",
       "      <td>22.6500</td>\n",
       "      <td>4.530000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>hc</th>\n",
       "      <td>9.217307</td>\n",
       "      <td>9.0</td>\n",
       "      <td>37.0260</td>\n",
       "      <td>4.813380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ni</th>\n",
       "      <td>3.910231</td>\n",
       "      <td>4.0</td>\n",
       "      <td>50.0840</td>\n",
       "      <td>9.515960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>bu</th>\n",
       "      <td>15.669765</td>\n",
       "      <td>16.0</td>\n",
       "      <td>58.5280</td>\n",
       "      <td>8.779200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FG</th>\n",
       "      <td>21.282453</td>\n",
       "      <td>21.0</td>\n",
       "      <td>80.3460</td>\n",
       "      <td>7.231140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SR</th>\n",
       "      <td>12.735056</td>\n",
       "      <td>13.0</td>\n",
       "      <td>81.9910</td>\n",
       "      <td>5.739370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RM</th>\n",
       "      <td>40.390244</td>\n",
       "      <td>40.0</td>\n",
       "      <td>114.8000</td>\n",
       "      <td>17.220000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>cs</th>\n",
       "      <td>45.516276</td>\n",
       "      <td>46.0</td>\n",
       "      <td>131.4220</td>\n",
       "      <td>11.827980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>lu</th>\n",
       "      <td>33.915604</td>\n",
       "      <td>34.0</td>\n",
       "      <td>138.5840</td>\n",
       "      <td>16.630080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>59.229175</td>\n",
       "      <td>59.0</td>\n",
       "      <td>142.3670</td>\n",
       "      <td>17.084040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>m</th>\n",
       "      <td>48.982795</td>\n",
       "      <td>49.0</td>\n",
       "      <td>162.3370</td>\n",
       "      <td>16.233700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sc</th>\n",
       "      <td>3.185554</td>\n",
       "      <td>3.0</td>\n",
       "      <td>162.8100</td>\n",
       "      <td>24.421500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UR</th>\n",
       "      <td>40.672348</td>\n",
       "      <td>41.0</td>\n",
       "      <td>173.1840</td>\n",
       "      <td>17.318400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SA</th>\n",
       "      <td>42.788650</td>\n",
       "      <td>43.0</td>\n",
       "      <td>175.7840</td>\n",
       "      <td>15.820560</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>fu</th>\n",
       "      <td>66.106557</td>\n",
       "      <td>66.0</td>\n",
       "      <td>193.2480</td>\n",
       "      <td>28.987200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>i</th>\n",
       "      <td>25.262513</td>\n",
       "      <td>25.0</td>\n",
       "      <td>244.7500</td>\n",
       "      <td>31.817500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          qty  qty取整        市值        保证金\n",
       "品种                                       \n",
       "ru -20.299224  -20.0 -283.4000  28.340000\n",
       "eg -40.018067  -40.0 -177.1200  21.254400\n",
       "lh  -4.865111   -5.0 -109.7200  13.166400\n",
       "sn  -4.939180   -5.0 -106.0500  14.847000\n",
       "sp -17.872340  -18.0 -101.5200  15.228000\n",
       "SF -29.447115  -29.0  -96.5120  11.581440\n",
       "v  -32.037195  -32.0  -94.0640  10.347040\n",
       "bc  -3.103022   -3.0  -92.3100   9.231000\n",
       "nr  -8.476276   -8.0  -89.3600   8.936000\n",
       "eb -17.095710  -17.0  -72.1140   7.211400\n",
       "cu  -1.628555   -2.0  -68.9200   8.270400\n",
       "ss  -9.514620  -10.0  -68.4000   6.156000\n",
       "zn  -6.037596   -6.0  -64.6350   9.048900\n",
       "ag  -6.033021   -6.0  -53.7840   6.454080\n",
       "TA -18.423181  -18.0  -53.4240   4.273920\n",
       "SM -11.907152  -12.0  -38.2560   4.590720\n",
       "pb  -3.774488   -4.0  -31.7500   4.445000\n",
       "pg  -2.989992   -3.0  -28.7760   3.740880\n",
       "CF  -3.038499   -3.0  -23.1825   1.622775\n",
       "l   -5.120773   -5.0  -20.7000   2.277000\n",
       "al  -2.239426   -2.0  -19.5050   2.340600\n",
       "PF  -4.196185   -4.0  -14.6800   1.468000\n",
       "p   -1.721705   -2.0  -14.1720   1.700640\n",
       "OI  -1.416196   -1.0   -7.9650   0.716850\n",
       "y   -1.063547   -1.0   -7.5220   0.676980\n",
       "au   0.161927    0.0    0.0000   0.000000\n",
       "a    0.649819    1.0    4.9860   0.598320\n",
       "pp   2.116402    2.0    7.5600   0.831600\n",
       "rb   1.885724    2.0    8.0040   1.040520\n",
       "b    3.690476    4.0   17.4720   1.572480\n",
       "MA   9.230769    9.0   21.8790   1.750320\n",
       "jm   1.769536    2.0   22.6500   4.530000\n",
       "hc   9.217307    9.0   37.0260   4.813380\n",
       "ni   3.910231    4.0   50.0840   9.515960\n",
       "bu  15.669765   16.0   58.5280   8.779200\n",
       "FG  21.282453   21.0   80.3460   7.231140\n",
       "SR  12.735056   13.0   81.9910   5.739370\n",
       "RM  40.390244   40.0  114.8000  17.220000\n",
       "cs  45.516276   46.0  131.4220  11.827980\n",
       "lu  33.915604   34.0  138.5840  16.630080\n",
       "c   59.229175   59.0  142.3670  17.084040\n",
       "m   48.982795   49.0  162.3370  16.233700\n",
       "sc   3.185554    3.0  162.8100  24.421500\n",
       "UR  40.672348   41.0  173.1840  17.318400\n",
       "SA  42.788650   43.0  175.7840  15.820560\n",
       "fu  66.106557   66.0  193.2480  28.987200\n",
       "i   25.262513   25.0  244.7500  31.817500"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_qty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c9fa2b06",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>qty</th>\n",
       "      <th>qty取整</th>\n",
       "      <th>市值</th>\n",
       "      <th>保证金</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>品种</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ru</th>\n",
       "      <td>-7.104728</td>\n",
       "      <td>-7.0</td>\n",
       "      <td>-99.1900</td>\n",
       "      <td>9.919000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>eg</th>\n",
       "      <td>-14.006323</td>\n",
       "      <td>-14.0</td>\n",
       "      <td>-61.9920</td>\n",
       "      <td>7.439040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>lh</th>\n",
       "      <td>-1.702789</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>-43.8880</td>\n",
       "      <td>5.266560</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sn</th>\n",
       "      <td>-1.728713</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>-42.4200</td>\n",
       "      <td>5.938800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>cu</th>\n",
       "      <td>-0.569994</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-34.4600</td>\n",
       "      <td>4.135200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sp</th>\n",
       "      <td>-6.255319</td>\n",
       "      <td>-6.0</td>\n",
       "      <td>-33.8400</td>\n",
       "      <td>5.076000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>nr</th>\n",
       "      <td>-2.966697</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>-33.5100</td>\n",
       "      <td>3.351000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SF</th>\n",
       "      <td>-10.306490</td>\n",
       "      <td>-10.0</td>\n",
       "      <td>-33.2800</td>\n",
       "      <td>3.993600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>v</th>\n",
       "      <td>-11.213018</td>\n",
       "      <td>-11.0</td>\n",
       "      <td>-32.3345</td>\n",
       "      <td>3.556795</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>bc</th>\n",
       "      <td>-1.086058</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-30.7700</td>\n",
       "      <td>3.077000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>eb</th>\n",
       "      <td>-5.983498</td>\n",
       "      <td>-6.0</td>\n",
       "      <td>-25.4520</td>\n",
       "      <td>2.545200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>zn</th>\n",
       "      <td>-2.113159</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>-21.5450</td>\n",
       "      <td>3.016300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ss</th>\n",
       "      <td>-3.330117</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>-20.5200</td>\n",
       "      <td>1.846800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ag</th>\n",
       "      <td>-2.111557</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>-17.9280</td>\n",
       "      <td>2.151360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TA</th>\n",
       "      <td>-6.448113</td>\n",
       "      <td>-6.0</td>\n",
       "      <td>-17.8080</td>\n",
       "      <td>1.424640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SM</th>\n",
       "      <td>-4.167503</td>\n",
       "      <td>-4.0</td>\n",
       "      <td>-12.7520</td>\n",
       "      <td>1.530240</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>al</th>\n",
       "      <td>-0.783799</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-9.7525</td>\n",
       "      <td>1.170300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pg</th>\n",
       "      <td>-1.046497</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-9.5920</td>\n",
       "      <td>1.246960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>l</th>\n",
       "      <td>-1.792271</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>-8.2800</td>\n",
       "      <td>0.910800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pb</th>\n",
       "      <td>-1.321071</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-7.9375</td>\n",
       "      <td>1.111250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CF</th>\n",
       "      <td>-1.063475</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-7.7275</td>\n",
       "      <td>0.540925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>p</th>\n",
       "      <td>-0.602597</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-7.0860</td>\n",
       "      <td>0.850320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PF</th>\n",
       "      <td>-1.468665</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-3.6700</td>\n",
       "      <td>0.367000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>OI</th>\n",
       "      <td>-0.495669</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0.227437</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>-0.372241</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>au</th>\n",
       "      <td>0.056674</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pp</th>\n",
       "      <td>0.740741</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.7800</td>\n",
       "      <td>0.415800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rb</th>\n",
       "      <td>0.660003</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0020</td>\n",
       "      <td>0.520260</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>1.291667</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.3680</td>\n",
       "      <td>0.393120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MA</th>\n",
       "      <td>3.230769</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.2930</td>\n",
       "      <td>0.583440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>jm</th>\n",
       "      <td>0.619338</td>\n",
       "      <td>1.0</td>\n",
       "      <td>11.3250</td>\n",
       "      <td>2.265000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>hc</th>\n",
       "      <td>3.226057</td>\n",
       "      <td>3.0</td>\n",
       "      <td>12.3420</td>\n",
       "      <td>1.604460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ni</th>\n",
       "      <td>1.368581</td>\n",
       "      <td>1.0</td>\n",
       "      <td>12.5210</td>\n",
       "      <td>2.378990</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>bu</th>\n",
       "      <td>5.484418</td>\n",
       "      <td>5.0</td>\n",
       "      <td>18.2900</td>\n",
       "      <td>2.743500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SR</th>\n",
       "      <td>4.457270</td>\n",
       "      <td>4.0</td>\n",
       "      <td>25.2280</td>\n",
       "      <td>1.765960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FG</th>\n",
       "      <td>7.448859</td>\n",
       "      <td>7.0</td>\n",
       "      <td>26.7820</td>\n",
       "      <td>2.410380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RM</th>\n",
       "      <td>14.136585</td>\n",
       "      <td>14.0</td>\n",
       "      <td>40.1800</td>\n",
       "      <td>6.027000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>cs</th>\n",
       "      <td>15.930697</td>\n",
       "      <td>16.0</td>\n",
       "      <td>45.7120</td>\n",
       "      <td>4.114080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>lu</th>\n",
       "      <td>11.870461</td>\n",
       "      <td>12.0</td>\n",
       "      <td>48.9120</td>\n",
       "      <td>5.869440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>20.730211</td>\n",
       "      <td>21.0</td>\n",
       "      <td>50.6730</td>\n",
       "      <td>6.080760</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sc</th>\n",
       "      <td>1.114944</td>\n",
       "      <td>1.0</td>\n",
       "      <td>54.2700</td>\n",
       "      <td>8.140500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>m</th>\n",
       "      <td>17.143978</td>\n",
       "      <td>17.0</td>\n",
       "      <td>56.3210</td>\n",
       "      <td>5.632100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UR</th>\n",
       "      <td>14.235322</td>\n",
       "      <td>14.0</td>\n",
       "      <td>59.1360</td>\n",
       "      <td>5.913600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SA</th>\n",
       "      <td>14.976027</td>\n",
       "      <td>15.0</td>\n",
       "      <td>61.3200</td>\n",
       "      <td>5.518800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>fu</th>\n",
       "      <td>23.137295</td>\n",
       "      <td>23.0</td>\n",
       "      <td>67.3440</td>\n",
       "      <td>10.101600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>i</th>\n",
       "      <td>8.841879</td>\n",
       "      <td>9.0</td>\n",
       "      <td>88.1100</td>\n",
       "      <td>11.454300</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          qty  qty取整       市值        保证金\n",
       "品种                                      \n",
       "ru  -7.104728   -7.0 -99.1900   9.919000\n",
       "eg -14.006323  -14.0 -61.9920   7.439040\n",
       "lh  -1.702789   -2.0 -43.8880   5.266560\n",
       "sn  -1.728713   -2.0 -42.4200   5.938800\n",
       "cu  -0.569994   -1.0 -34.4600   4.135200\n",
       "sp  -6.255319   -6.0 -33.8400   5.076000\n",
       "nr  -2.966697   -3.0 -33.5100   3.351000\n",
       "SF -10.306490  -10.0 -33.2800   3.993600\n",
       "v  -11.213018  -11.0 -32.3345   3.556795\n",
       "bc  -1.086058   -1.0 -30.7700   3.077000\n",
       "eb  -5.983498   -6.0 -25.4520   2.545200\n",
       "zn  -2.113159   -2.0 -21.5450   3.016300\n",
       "ss  -3.330117   -3.0 -20.5200   1.846800\n",
       "ag  -2.111557   -2.0 -17.9280   2.151360\n",
       "TA  -6.448113   -6.0 -17.8080   1.424640\n",
       "SM  -4.167503   -4.0 -12.7520   1.530240\n",
       "al  -0.783799   -1.0  -9.7525   1.170300\n",
       "pg  -1.046497   -1.0  -9.5920   1.246960\n",
       "l   -1.792271   -2.0  -8.2800   0.910800\n",
       "pb  -1.321071   -1.0  -7.9375   1.111250\n",
       "CF  -1.063475   -1.0  -7.7275   0.540925\n",
       "p   -0.602597   -1.0  -7.0860   0.850320\n",
       "PF  -1.468665   -1.0  -3.6700   0.367000\n",
       "OI  -0.495669   -0.0  -0.0000   0.000000\n",
       "a    0.227437    0.0   0.0000   0.000000\n",
       "y   -0.372241   -0.0  -0.0000   0.000000\n",
       "au   0.056674    0.0   0.0000   0.000000\n",
       "pp   0.740741    1.0   3.7800   0.415800\n",
       "rb   0.660003    1.0   4.0020   0.520260\n",
       "b    1.291667    1.0   4.3680   0.393120\n",
       "MA   3.230769    3.0   7.2930   0.583440\n",
       "jm   0.619338    1.0  11.3250   2.265000\n",
       "hc   3.226057    3.0  12.3420   1.604460\n",
       "ni   1.368581    1.0  12.5210   2.378990\n",
       "bu   5.484418    5.0  18.2900   2.743500\n",
       "SR   4.457270    4.0  25.2280   1.765960\n",
       "FG   7.448859    7.0  26.7820   2.410380\n",
       "RM  14.136585   14.0  40.1800   6.027000\n",
       "cs  15.930697   16.0  45.7120   4.114080\n",
       "lu  11.870461   12.0  48.9120   5.869440\n",
       "c   20.730211   21.0  50.6730   6.080760\n",
       "sc   1.114944    1.0  54.2700   8.140500\n",
       "m   17.143978   17.0  56.3210   5.632100\n",
       "UR  14.235322   14.0  59.1360   5.913600\n",
       "SA  14.976027   15.0  61.3200   5.518800\n",
       "fu  23.137295   23.0  67.3440  10.101600\n",
       "i    8.841879    9.0  88.1100  11.454300"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_qty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "427f70ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "ddd_qty = (pd.Series([0]*len(字母s),index=字母s) + df_qty['qty取整']).fillna(0).sort_values()\n",
    "\n",
    "account_names = []\n",
    "strategys = []\n",
    "symbols = []\n",
    "setnos = []\n",
    "qtys = []\n",
    "trading_algos = []\n",
    "for i in range(len(ddd_qty)):\n",
    "    strategy = 'BuyOrSell'\n",
    "    品种 = ddd_qty.index[i]\n",
    "    symbol = 品种 + '00'\n",
    "    if ddd_qty[品种] < 0:\n",
    "        setno = 3\n",
    "    elif ddd_qty[品种] > 0:\n",
    "        setno = 2\n",
    "    else:\n",
    "        setno = 1\n",
    "        \n",
    "    qty = abs(ddd_qty[品种])\n",
    "    trading_algo = 'GradientChaseAlgo'\n",
    "    account_names.append(account_name)\n",
    "    strategys.append(strategy)\n",
    "    symbols.append(symbol)\n",
    "    setnos.append(setno)\n",
    "    qtys.append(qty)\n",
    "    trading_algos.append(trading_algo)\n",
    "    \n",
    "cta_strategy_setting_bos = pd.DataFrame({'account_name':account_names,\n",
    "                                     'strategy':strategys,\n",
    "                                     'symbol':symbols,\n",
    "                                     'setno':setnos,\n",
    "                                     'qty':qtys,\n",
    "                                     'trading_algo':trading_algos})\n",
    "\n",
    "cta_strategy_setting_bos = cta_strategy_setting_bos[cta_strategy_setting_bos['setno']!=1]\n",
    "cta_strategy_setting_bos = cta_strategy_setting_bos.reset_index(drop=True)\n",
    "cta_strategy_setting_bos['ls'] = cta_strategy_setting_bos['symbol'] + '_' + cta_strategy_setting_bos['setno'].astype(str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c59144bc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e1ae0d24",
   "metadata": {},
   "outputs": [],
   "source": [
    "cta_strategy_setting_old = pd.read_csv('cta上线交易单元配置参数/cta_strategy_setting.csv')\n",
    "cta_strategy_setting_old = cta_strategy_setting_old[cta_strategy_setting_old['qty']!=0]\n",
    "cta_strategy_setting_old_non_bos = cta_strategy_setting_old[cta_strategy_setting_old['strategy']!='BuyOrSell']\n",
    "cta_strategy_setting_old_bos = cta_strategy_setting_old[cta_strategy_setting_old['strategy']=='BuyOrSell']\n",
    "cta_strategy_setting_old_bos = cta_strategy_setting_old_bos[cta_strategy_setting_old_bos['account_name']==account_name]\n",
    "cta_strategy_setting_old_bos = cta_strategy_setting_old_bos.reset_index(drop=True)\n",
    "cta_strategy_setting_old_bos['ls'] = cta_strategy_setting_old_bos['symbol'] + '_' + cta_strategy_setting_old_bos['setno'].astype(str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "159845bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "x1s = []\n",
    "x2s = []\n",
    "x3s = []\n",
    "x4s = []\n",
    "x5s = []\n",
    "x6s = []\n",
    "x7s = []\n",
    "for i in range(len(cta_strategy_setting_old_bos)):\n",
    "    if cta_strategy_setting_old_bos.loc[i,'ls'] not in list(cta_strategy_setting_bos['ls']):\n",
    "        x1s.append(account_name)\n",
    "        x2s.append('BuyOrSell')\n",
    "        x3s.append(cta_strategy_setting_old_bos.loc[i,'symbol'])\n",
    "        x4s.append(cta_strategy_setting_old_bos.loc[i,'setno'])\n",
    "        x5s.append(0)\n",
    "        x6s.append('GradientChaseAlgo')\n",
    "        x7s.append(cta_strategy_setting_old_bos.loc[i,'ls'])\n",
    "清仓df = pd.DataFrame({'account_name':x1s,'strategy':'BuyOrSell','symbol':x3s,'setno':x4s,\n",
    "                     'qty':x5s,'trading_algo':x6s,'ls':x7s})\n",
    "cta_strategy_setting_bos = pd.concat([cta_strategy_setting_bos,清仓df])\n",
    "cta_strategy_setting_bos[['setno','qty']] = cta_strategy_setting_bos[['setno','qty']].astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d48710f4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "cta_strategy_setting_bos = cta_strategy_setting_bos[~cta_strategy_setting_bos['symbol'].isin(['T00','TF00','TS00'])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "85188d60",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 国债df = pd.DataFrame({\n",
    "#     'account_name':['CP','CP','CP'],\n",
    "#     'strategy':['BuyOrSell','BuyOrSell','BuyOrSell'],\n",
    "#     'symbol':['T00','TF00','TS00'],\n",
    "#     'setno':[2,2,2],\n",
    "#     'qty':[8,8,4],\n",
    "#     'trading_algo':['GradientChaseAlgo','GradientChaseAlgo','GradientChaseAlgo']\n",
    "# })\n",
    "\n",
    "# 国债df = pd.DataFrame({\n",
    "#     'account_name':['DS'],\n",
    "#     'strategy':['BuyOrSell'],\n",
    "#     'symbol':['TF00'],\n",
    "#     'setno':[2],\n",
    "#     'qty':[8],\n",
    "#     'trading_algo':['GradientChaseAlgo']\n",
    "# })\n",
    "\n",
    "国债df = pd.DataFrame({\n",
    "    'account_name':['DS','DS','DS'],\n",
    "    'strategy':['BuyOrSell','BuyOrSell','BuyOrSell'],\n",
    "    'symbol':['T00','TF00','TS00'],\n",
    "    'setno':[2,2,2],\n",
    "    'qty':[3,2,2],\n",
    "    'trading_algo':['GradientChaseAlgo','GradientChaseAlgo','GradientChaseAlgo']\n",
    "})\n",
    "\n",
    "cta_strategy_setting_bos = pd.concat([cta_strategy_setting_bos,国债df])\n",
    "# cta_strategy_setting_bos = cta_strategy_setting_bos.drop(['ls'],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a39e9a0f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "cta_strategy_setting_bos.to_csv(f'strategy_setting_{account_name}.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0caf60d8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "f1c4db51",
   "metadata": {},
   "outputs": [],
   "source": [
    "cta_strategy_setting_CP = pd.read_csv('strategy_setting_CP.csv')\n",
    "cta_strategy_setting_DS = pd.read_csv('strategy_setting_DS.csv')\n",
    "cta_strategy_setting = pd.concat([cta_strategy_setting_CP,cta_strategy_setting_DS])\n",
    "cta_strategy_setting = cta_strategy_setting.drop(['ls'],axis=1)\n",
    "cta_strategy_setting.to_csv('cta上线交易单元配置参数/cta_strategy_setting_20231229.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f1994cfe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account_name</th>\n",
       "      <th>strategy</th>\n",
       "      <th>symbol</th>\n",
       "      <th>setno</th>\n",
       "      <th>qty</th>\n",
       "      <th>trading_algo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CP</td>\n",
       "      <td>BuyOrSell</td>\n",
       "      <td>eg00</td>\n",
       "      <td>3</td>\n",
       "      <td>40</td>\n",
       "      <td>GradientChaseAlgo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CP</td>\n",
       "      <td>BuyOrSell</td>\n",
       "      <td>v00</td>\n",
       "      <td>3</td>\n",
       "      <td>32</td>\n",
       "      <td>GradientChaseAlgo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CP</td>\n",
       "      <td>BuyOrSell</td>\n",
       "      <td>SF00</td>\n",
       "      <td>3</td>\n",
       "      <td>29</td>\n",
       "      <td>GradientChaseAlgo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CP</td>\n",
       "      <td>BuyOrSell</td>\n",
       "      <td>ru00</td>\n",
       "      <td>3</td>\n",
       "      <td>20</td>\n",
       "      <td>GradientChaseAlgo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CP</td>\n",
       "      <td>BuyOrSell</td>\n",
       "      <td>TA00</td>\n",
       "      <td>3</td>\n",
       "      <td>18</td>\n",
       "      <td>GradientChaseAlgo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>DS</td>\n",
       "      <td>BuyOrSell</td>\n",
       "      <td>l00</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>GradientChaseAlgo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>DS</td>\n",
       "      <td>BuyOrSell</td>\n",
       "      <td>au00</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>GradientChaseAlgo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53</th>\n",
       "      <td>DS</td>\n",
       "      <td>BuyOrSell</td>\n",
       "      <td>T00</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>GradientChaseAlgo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>DS</td>\n",
       "      <td>BuyOrSell</td>\n",
       "      <td>TF00</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>GradientChaseAlgo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>DS</td>\n",
       "      <td>BuyOrSell</td>\n",
       "      <td>TS00</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>GradientChaseAlgo</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>115 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   account_name   strategy symbol  setno  qty       trading_algo\n",
       "0            CP  BuyOrSell   eg00      3   40  GradientChaseAlgo\n",
       "1            CP  BuyOrSell    v00      3   32  GradientChaseAlgo\n",
       "2            CP  BuyOrSell   SF00      3   29  GradientChaseAlgo\n",
       "3            CP  BuyOrSell   ru00      3   20  GradientChaseAlgo\n",
       "4            CP  BuyOrSell   TA00      3   18  GradientChaseAlgo\n",
       "..          ...        ...    ...    ...  ...                ...\n",
       "51           DS  BuyOrSell    l00      2    0  GradientChaseAlgo\n",
       "52           DS  BuyOrSell   au00      2    0  GradientChaseAlgo\n",
       "53           DS  BuyOrSell    T00      2    3  GradientChaseAlgo\n",
       "54           DS  BuyOrSell   TF00      2    2  GradientChaseAlgo\n",
       "55           DS  BuyOrSell   TS00      2    2  GradientChaseAlgo\n",
       "\n",
       "[115 rows x 6 columns]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cta_strategy_setting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76b25e45",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b496e72",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16faf713",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
